//
//  NGSNoodle.m
//  demo_3
//
//  Created by 才 陈 on 12-3-5.
//  Copyright (c) 2012年 sjtu. All rights reserved.
//

#import "commons.h"
#import "NGSNoodle.h"
#import "NoodleType.h"

static NSDictionary *NOODLE_TYPES;
static int MAX_SCORE;

@implementation NGSNoodle

@synthesize type;
@synthesize count;
@synthesize name;

-(id)init:(NoodleType *) noodleType WithCount:(int)cnt
{
    if(self=[super init])
    {
        type = noodleType;
        count = cnt;
    }
    return self;
}



-(void) addCount:(int)step
{
    count+=step;
}





+(NSString *) stringOfType:(NoodleType *)type
{
    return [NSString stringWithFormat:@"%d", type];
}

+(int) stringToType:(NSString *)typeValue
{
    return [typeValue intValue];
}


+(NSDictionary *) NOODLE_TYPES
{
    if(NOODLE_TYPES==Nil)
    {
        NOODLE_TYPES = [[NSMutableDictionary alloc] init];
        
        NSString *propsFilePath=[[NSBundle mainBundle] pathForResource:@"prescription" ofType:@"properties"];
        
        NSDictionary *props = [NGSUtils loadProperties:propsFilePath :YES];
        
        NSDictionary *ingredientTypes = [IngredientType ingredientTypes:props];
        NSDictionary *noodleTypes = [NoodleType noodleTypes:props :ingredientTypes];
        
        for (NSString *key in [noodleTypes allKeys]) {
            NoodleType *noodleType = [noodleTypes valueForKey:key];
            NGSNoodle *bowlOfNoodle = [[NGSNoodle alloc] init:noodleType WithCount:0];
            
            NSString *typeName = noodleType.typeName;
            [NOODLE_TYPES setValue:bowlOfNoodle forKey:typeName];
        }
        /*
        NOODLE_TYPES = [NSMutableDictionary dictionaryWithCapacity:6];
        NGSNoodle *beefNoodle = [[NGSNoodle alloc] init:TYPE_BEEF_NOODLE WithName:@"牛肉刀削面"];
        [NOODLE_TYPES setObject:beefNoodle forKey:[NSNumber numberWithInt:TYPE_BEEF_NOODLE]];
        
        NGSNoodle *muttonNoodle = [[NGSNoodle alloc] init:TYPE_MUTTON_NOODLE WithName:@"羊肉刀削面"];
        [NOODLE_TYPES setObject:muttonNoodle forKey:[NSNumber numberWithInt:TYPE_MUTTON_NOODLE]];
        
        NGSNoodle *radishNoodle = [[NGSNoodle alloc] init:TYPE_RADISH_NOODLE WithName:@"萝卜刀削面"];
        [NOODLE_TYPES setObject:radishNoodle forKey:[NSNumber numberWithInt:TYPE_RADISH_NOODLE]];
        
        NGSNoodle *radishBeefNoodle = [[NGSNoodle alloc] init:TYPE_RADISH_BEEF_NOODLE WithName:@"萝卜牛肉刀削面"];
        [NOODLE_TYPES setObject:radishBeefNoodle forKey:[NSNumber numberWithInt:TYPE_RADISH_BEEF_NOODLE]];
        
        NGSNoodle *radishMuttonNoodle = [[NGSNoodle alloc] init:TYPE_RADISH_MUTTON_NOODLE WithName:@"萝卜羊肉刀削面"];
        [NOODLE_TYPES setObject:radishMuttonNoodle forKey:[NSNumber numberWithInt:TYPE_RADISH_MUTTON_NOODLE]];
         */
    }
    return NOODLE_TYPES;
}

+(int) totalScore
{
    int total = 0;
    for (NGSNoodle *key in [NOODLE_TYPES allKeys])
    {
        NGSNoodle *bowlOfNoodle = [NOODLE_TYPES objectForKey:key];
        total += bowlOfNoodle.type.score*bowlOfNoodle.count;
    }
    return total;
}

+(int) maxScore
{
    if(MAX_SCORE>0) return MAX_SCORE;
    else return 0;
}

+(void) updateMaxScore:(int)score
{
    if(score>MAX_SCORE) MAX_SCORE = score;
}

@end
